package com.zeno.chinesecalendar.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author zeno.w
 * @date 2021/11/27 16:26
 */
class YearWorkBook {

    private volatile CellStyle hourCellStyle;
    private volatile CellStyle dayCellStyle;
    private volatile CellStyle titleCellStyle;
    private final String FILE_SUFFIX = "年.xlsx";

    private Workbook wb;
    private int year;

    public YearWorkBook(int year){
        this.year = year;
        this.wb = new XSSFWorkbook();
    }

    public void flush(){
        try (OutputStream fileOut = new FileOutputStream(this.year + FILE_SUFFIX)){
            wb.write(fileOut);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    MonthSheet addMonthSheet(int month){
        return new MonthSheet(this,this.year,month);
    }

    Sheet createSheet(String name){
        return wb.createSheet(name);
    }

    public CellStyle titleCellStyle(){

        if (titleCellStyle == null){
            titleCellStyle = wb.createCellStyle();
            titleCellStyle.setAlignment(HorizontalAlignment.LEFT);
            titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            Font font = wb.createFont();
            font.setFontHeight((short)280);
            font.setBold(true);
            titleCellStyle.setFont(font);
        }
        return titleCellStyle;
    }

    public CellStyle dayCellStyle(){

        if (dayCellStyle == null){
            dayCellStyle = wb.createCellStyle();
            dayCellStyle.setAlignment(HorizontalAlignment.CENTER);
            dayCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            dayCellStyle.setWrapText(true);
        }
        return dayCellStyle;
    }

    public CellStyle hourCellStyle(){

        if (hourCellStyle == null){
            hourCellStyle = wb.createCellStyle();
            hourCellStyle.setAlignment(HorizontalAlignment.CENTER);
            hourCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        }
        return hourCellStyle;
    }
}
